package com.tbit.main.controller;

import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.enums.OperaPswEnum;
import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.OperaPswConstant;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.controller.interceptor.LoginUserContextHolder;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.PageResult;
import com.tbit.main.pojo.RideLog;
import com.tbit.main.pojo.VO.EditOrderReqVO;
import com.tbit.main.pojo.VO.RideLogReqVO;
import com.tbit.main.pojo.VO.RideLogRespVO;
import com.tbit.main.service.OperaPswService;
import com.tbit.main.service.RideLogService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 订单查询
 *
 * @author LMY
 * @since 2021-03-30 14:32
 */
@Validated
@RestController
@RequestMapping("/rideLog")
public class RideLogController {

    @Resource
    private TokenService tokenService;
    @Resource
    private RideLogService rideLogService;
    @Autowired
    private OperaPswService operaPswService;

    /**
     * 根据车辆编号查询
     *
     * @return List {@link RideLog}
     */
    @RequestMapping("/getUserCode")
    public JsonResult<List<RideLog>> getUserCode(String token, String codePhone, String startTime, String endTime) {
        Assert.hasLength(startTime, "System.startTime.notNull");
        Assert.hasLength(endTime, "System.endTime.notNull");
        Assert.hasLength(codePhone, "CodePhone.notNull");
        AccountUser accountUser = tokenService.getAccount(token);

        List<RideLog> rideLogList = rideLogService.getUserCode(codePhone, startTime, endTime, accountUser);

        return JsonResult.succ(rideLogList);
    }

    /**
     * 输入手机号搜索用户骑行订单详情 （未选区域查询登录用户下的所持有区域）
     * 输入车辆编号搜索车辆骑行订单详情
     * 输入订单编号搜索该笔订单详情
     *
     * @param reqVO 请求
     */
    @GetMapping("/getRideLogPage")
    public JsonResult<PageResult<RideLogRespVO>> getRideLogPage(@Valid RideLogReqVO reqVO) {
        return JsonResult.succ(rideLogService.getRideLogPage(reqVO));
    }
    /**
     * 查询用户所有骑行订单详情
     *
     * @param userId 用户id
     */
    @GetMapping("/getRideLogByUserId")
    public JsonResult<PageResult<RideLogRespVO>> getRideLogPage(@NotNull Integer userId,@NotNull Integer pageNum,
                                                                @NotNull Integer pageSize) {
        return JsonResult.succ(rideLogService.getRideLogByUserId(userId, pageNum, pageSize));
    }

    /**
     * 根据车辆编号查询最近骑行用户
     *
     * @param machineId 设备id
     * @param regionId 分区id
     * @return List {@link RideLog}
     */
    @RequestMapping("/getRecentUser")
    public JsonResult<List<RideLog>> getRecentUser(Integer machineId, Integer regionId,
                                                   @RequestParam(defaultValue = "1") Integer pageNO,
                                                   @RequestParam(defaultValue = "5")Integer rowCount) {
        Assert.notNull(machineId, "Machine.machineId.notNull");
        List<RideLog> rideLogList = rideLogService.getRecentUser(machineId,regionId,pageNO,rowCount);
        return JsonResult.succ(rideLogList);
    }

    /**
     * 修改订单金额
     * @param reqVO 请求
     */
    @PostMapping("/editOrder")
    @OperateLog(module = ModuleEnum.ORDER, methods = MethodEnum.ORDER_EDIT)
    public JsonResult<Object> editOrder(@Valid EditOrderReqVO reqVO) {
        AccountUser loginUser = LoginUserContextHolder.getLoginUser();
        /**操作密码校验 兼容5.0.0之前版本 常量校验不通过时，再调接口校验*/
        if (!OperaPswConstant.USER_OPERA_UPDATE_MONEY.equals(reqVO.getPsw())) {
            operaPswService.checkOperaPsw(loginUser, OperaPswEnum.OPERA_PSW_MONEY_CONFIG.getModule(), reqVO.getPsw());
        }

        rideLogService.editOrder(reqVO);
        return JsonResult.succ();
    }
}
